package com.toft.widgets.workflow.adpater;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;

import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;

import com.toft.core2.dao.SqlUtils;
import com.toft.widgets.workflow.utils.ConstantUtil;
import com.toft.widgets.workflow.utils.ReflectionUtils;
import com.toft.widgets.workflow.utils.TableConstants;

public class WorkflowQueryAdapter {

	protected static CacheManager manager = CacheManager.getInstance();

	public static WorkflowQueryAdapter getInstance() {

		ResourceBundle workflowPropertyies = ResourceBundle.getBundle("workflow");
		String queryClass = workflowPropertyies.getString("workflow.query.adapter");
		if (queryClass != null && !"".equals(queryClass)) {
			try {
				return (WorkflowQueryAdapter) Class.forName(queryClass).newInstance();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		return new WorkflowQueryAdapter();
	}

	/**
	 * 查询用户名
	 * 
	 * @param conditions
	 *            查询条件，包括用户标识、机构标识、部门标识以及数据库连接
	 * @return 返回用户名称
	 */
	public List getUserListByCode(Map conditions) {

		List list = new ArrayList();
		if (null != conditions) {
			String code = (String) conditions.get("code");
			String agencyId = (String) conditions.get("agencyId");
			String departmentId = (String) conditions.get("departmentId");
			Connection conn = (Connection) conditions.get("conn");
			String submitUser = (String) conditions.get(ConstantUtil.SUBMIT_USER);

			String sql = "SELECT U.NAME AS NAME,U.FLAG AS FLAG,U.STATUS AS STATUS FROM " + TableConstants.USER_TABLE + " U WHERE U.CODE='" + code
					+ "' and U.STATUS=1";
			try {
				Cache cache = manager.getCache("userInfo");
				if (cache != null) {
					Element element = cache.get(code);
					if (element == null) {
						list = SqlUtils.select(sql, conn);
						element = new Element(code, list);
						cache.put(element);
					} else {
						list = (List) element.getValue();
					}
				} else
					list = SqlUtils.select(sql, conn);
			} catch (Exception e) {
				e.printStackTrace();
				throw new RuntimeException("查询用户名称异常" + e.getMessage());
			}

		}

		return list;
	}

	public String getUserImageByCode(Map conditions) {

		Map map = null;
		if (null != conditions) {
			String code = (String) conditions.get("code");
			Connection conn = (Connection) conditions.get("conn");
			String sql = "SELECT U.IMAGEPATH AS PATH FROM " + TableConstants.USER_TABLE + " U WHERE U.CODE='" + code + "' and U.STATUS=1";
			try {
				map = SqlUtils.selectOne(sql, conn);
			} catch (Exception e) {
				e.printStackTrace();
				throw new RuntimeException("查询用户名称异常" + e.getMessage());
			}
		}
		if (map != null)
			return (String) map.get("PATH");
		return "";
	}

	public String getUserNameByCode(Map conditions) {
		return ReflectionUtils.convertElementPropertyToString(getUserListByCode(conditions), "NAME", ",");
	}

	/**
	 * 根据条件获取用户信息
	 * 
	 * @param conditions
	 * @return
	 */
	public List getUserListByGroupId(Map conditions) {

		List list = new ArrayList();
		if (null != conditions) {
			String groupId = (String) conditions.get("groupId");
			String agencyId = (String) conditions.get("agencyId");
			String departmentId = (String) conditions.get("departmentId");
			Connection conn = (Connection) conditions.get("conn");
			String submitUser = (String) conditions.get(ConstantUtil.SUBMIT_USER);

			String sql = "SELECT U.NAME AS NAME,U.CODE AS CODE,U.FLAG AS FLAG,U.STATUS AS STATUS FROM " + TableConstants.USER_TABLE + " U,"
					+ TableConstants.USER_GROUP + " G WHERE U.Id=G.USERID and G.AGENCY_DUTY_ID='" + groupId + "' and U.STATUS=1";

			try {
				Cache cache = manager.getCache("userInfo");
				if (cache != null) {
					Element element = cache.get(groupId);
					if (element == null) {
						list = SqlUtils.select(sql, conn);
						element = new Element(groupId, list);
						cache.put(element);
					} else {
						list = (List) element.getValue();
					}
				} else
					list = SqlUtils.select(sql, conn);
			} catch (Exception e) {
				e.printStackTrace();
				throw new RuntimeException("查询组中的用户异常" + e.getMessage());
			}
		}
		return list;
	}

	public List getGroupIdsByUserCode(Map conditions) {

		List list = new ArrayList();

		if (null != conditions) {

			String agencyId = (String) conditions.get("agencyId");
			String departmentId = (String) conditions.get("departmentId");
			String code = (String) conditions.get("code");

			Connection conn = (Connection) conditions.get("conn");

			String sql = "SELECT AGENCY_DUTY_ID GROURPID FROM " + TableConstants.USER_GROUP + " UG WHERE UG.USERID in (SELECT ID FROM "
					+ TableConstants.USER_TABLE + " WHERE CODE='" + code + "' and STATUS=1)";
			try {
				Cache cache = manager.getCache("job");
				if (cache != null) {
					Element element = cache.get(code);
					if (element == null) {
						list = SqlUtils.select(sql, conn);
						element = new Element(code, list);
						cache.put(element);
					} else {
						list = (List) element.getValue();
					}
				} else
					list = SqlUtils.select(sql, conn);

			} catch (Exception e) {
				e.printStackTrace();
				throw new RuntimeException("查询组中的用户异常" + e.getMessage());
			}

		}
		return ReflectionUtils.convertElementPropertyToList(list, "GROURPID");

	}

	private void checkUserInfo(String userName, Map tempMap) {
		String flag = (String) tempMap.get("FLAG");
		String status = (String) tempMap.get("STATUS");
		if (!"1".equals(flag)) {
			throw new RuntimeException("用户" + userName + "已经被停用！");
		}
		if (!"1".equals(status)) {
			throw new RuntimeException("用户" + userName + "已经被删除！");
		}
	}
}
